package com.smyh.model.host.param;

import android.content.Context;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.smyh.bean.Enum.PointConsoleType;
import com.smyh.bean.Enum.PointDigitType;
import com.smyh.bean.Enum.PointMockType;
import com.smyh.bean.Enum.PointSetType;
import com.smyh.bean.PointObjInfo;
import com.smyh.cache.PointValueCache;
import com.smyh.cache.UIM.host.Host101PointState;
import com.smyh.model.tableModel.KtableView;
import com.smyh.util.BytesHex;

/**
 * Created by 姜龙 on 2016/7/8.
 */
public class Host101PointModel {

    private static TableLayout tableLayout1;
    private static TableLayout tableLayout2;
    private static TableLayout tableLayout3;
    private static TableLayout tableLayout4;

    /**
     * 获取遥信点号表单
     * @param context
     * @return
     */
    public static TableLayout getDigitTableLayout(Context context){
        if(tableLayout1 == null){
            tableLayout1 = new TableLayout(context);
            tableLayout1.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout1.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(null);
            String header0[] = {"点号","组号","类型","数据点"};
            /**
             * 遥信：132个点号 1-84
             * 遥测：4001-4078 :120个点号
             * 遥调：6201-623C :60个点号
             * 遥控：6001-603c :60个点号
             */

            tableLayout1.addView(KtableView.addTableHeaders(context,header0));
            String rowCell[][] = new String[132][4];
            for (int j = 0; j<rowCell.length;j++){

                byte[] pointV ={0, (byte) (j + 1)};
                String cellContent = BytesHex.bytesToHexString(pointV);
                rowCell[j][0] = cellContent;
                tableLayout1.addView(KtableView.addNewRow(context,rowCell[j]));
            }
        }
        return tableLayout1;
    }

    /**
     * 获取遥信点号表单
     * @param context
     * @return
     */
    public static TableLayout getMockTableLayout(Context context){
        if(tableLayout2 == null){
            tableLayout2 = new TableLayout(context);
            tableLayout2.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout2.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(null);
            String header0[] = {"点号","组号","类型","数据点"};
            /**
             * 遥信：132个点号 1-84
             * 遥测：4001-4078 :120个点号
             * 遥调：6201-623C :60个点号
             * 遥控：6001-603c :60个点号
             */


            tableLayout2.addView(KtableView.addTableHeaders(context,header0));
            String rowCell[][] = new String[120][4];
            for (int j = 0; j<rowCell.length;j++){

                byte[] pointV ={0x40, (byte) (j + 1)};
                String cellContent = BytesHex.bytesToHexString(pointV);
                rowCell[j][0] = cellContent;
                tableLayout2.addView(KtableView.addNewRow(context,rowCell[j]));
            }
        }
        return tableLayout2;
    }
    /**
     * 获取遥信点号表单
     * @param context
     * @return
     */
    public static TableLayout getSetTableLayout(Context context){
        if(tableLayout3 == null){
            tableLayout3 = new TableLayout(context);
            tableLayout3.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout3.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(null);
            String header0[] = {"点号","组号","类型","数据点"};
            /**
             * 遥信：132个点号 1-84
             * 遥测：4001-4078 :120个点号
             * 遥调：6201-623C :60个点号
             * 遥控：6001-603c :60个点号
             */


            tableLayout3.addView(KtableView.addTableHeaders(context,header0));
            String rowCell[][] = new String[60][4];
            for (int j = 0; j<rowCell.length;j++){

                byte[] pointV ={0x62, (byte) (j + 1)};
                String cellContent = BytesHex.bytesToHexString(pointV);
                rowCell[j][0] = cellContent;
                tableLayout3.addView(KtableView.addNewRow(context,rowCell[j]));
            }
        }
        return tableLayout3;
    }

    /**
     * 获取遥信点号表单
     * @param context
     * @return
     */
    public static TableLayout getControlTableLayout(Context context){
        if(tableLayout4 == null){
            tableLayout4 = new TableLayout(context);
            tableLayout4.setLayoutParams(new TableLayout.LayoutParams(
                    TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT));
            tableLayout4.setBackgroundColor(Color.BLACK);
            KtableView.setIsEditColNum(null);
            String header0[] = {"点号","组号","类型","数据点"};
            /**
             * 遥信：132个点号 1-84
             * 遥测：4001-4078 :120个点号
             * 遥调：6201-623C :60个点号
             * 遥控：6001-603c :60个点号
             */


            tableLayout4.addView(KtableView.addTableHeaders(context,header0));
            String rowCell[][] = new String[60][4];
            for (int j = 0; j<rowCell.length;j++){

                byte[] pointV ={0x60, (byte) (j + 1)};
                String cellContent = BytesHex.bytesToHexString(pointV);
                rowCell[j][0] = cellContent;
                tableLayout4.addView(KtableView.addNewRow(context,rowCell[j]));
            }
        }
        return tableLayout4;
    }


    /**
     *
     *
     * @param pointType:点位类型：
     */
    public static void setTableData(int pointSource ,int pointType) {

        switch (pointType){
            case 1:{
                int rowSize = tableLayout1.getChildCount();
                for (int i=1;i<rowSize;i++){
                    byte[] pointV ={0, (byte) i};
                    int pointValue = (pointV[0]&0xff)*256 + (pointV[1]&0xff);
                    PointObjInfo info = null;
                    if(pointSource == 1){
                        info = PointValueCache.pointDigitalMap.get(pointValue);
                    }else if(pointSource == 2){
                        info = PointValueCache.pointDigitalMap.get(pointValue);
                    }
                    TableRow childRow = (TableRow) tableLayout1.getChildAt(i);

                    if(info != null){
                        String groupStr = info.getGroupNum() == null?"": ""+info.getGroupNum();
                        ((TextView) childRow.getChildAt(1)).setText(groupStr);
                        ((TextView) childRow.getChildAt(2)).setText( PointDigitType.getName(info.getDataPoint()) == null?"":PointDigitType.getName(info.getDataPoint()));
                        String piontStr = info.getDataPoint() == null?"": ""+info.getDataPoint();
                        ((TextView) childRow.getChildAt(3)).setText(piontStr);

                    }
                }
                break;
            }
            case 2:{
                int rowSize = tableLayout2.getChildCount();
                for (int i=1;i<rowSize;i++){
                    byte[] pointV ={0x40, (byte) i};
                    int pointValue = (pointV[0]&0xff)*256 + (pointV[1]&0xff);
                    PointObjInfo info = PointValueCache.pointMockMap.get(pointValue);
                    TableRow childRow = (TableRow) tableLayout2.getChildAt(i);

                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(info.getGroupNum() == null?"": ""+info.getGroupNum());
                        ((TextView) childRow.getChildAt(2)).setText(PointMockType.getName(info.getDataPoint())== null?"":PointMockType.getName(info.getDataPoint()));
                        ((TextView) childRow.getChildAt(3)).setText(info.getDataPoint() == null?"": ""+info.getDataPoint());

                    }
                }
                break;
            }
            case 3:{
                int rowSize = tableLayout3.getChildCount();
                for (int i=1;i<rowSize;i++){
                    byte[] pointV ={0x62, (byte) i};
                    int pointValue = (pointV[0]&0xff)*256 + (pointV[1]&0xff);
                    PointObjInfo info = PointValueCache.pointSetMap.get(pointValue);
                    TableRow childRow = (TableRow) tableLayout3.getChildAt(i);

                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(info.getGroupNum() == null?"": ""+info.getGroupNum());
                        ((TextView) childRow.getChildAt(2)).setText(PointSetType.getName(info.getDataPoint())== null?"":PointSetType.getName(info.getDataPoint()));
                        ((TextView) childRow.getChildAt(3)).setText(info.getDataPoint() == null?"": ""+info.getDataPoint());

                    }
                }
                break;
            }
            case 4:{
                int rowSize = tableLayout4.getChildCount();
                for (int i=1;i<rowSize;i++){
                    byte[] pointV ={0x60, (byte)i};
                    int pointValue = (pointV[0]&0xff)*256 + (pointV[1]&0xff);
                    PointObjInfo info = PointValueCache.pointControlMap.get(pointValue);
                    TableRow childRow = (TableRow) tableLayout4.getChildAt(i);

                    if(info != null){
                        ((TextView) childRow.getChildAt(1)).setText(info.getGroupNum() == null?"": ""+info.getGroupNum());
                        ((TextView) childRow.getChildAt(2)).setText(PointConsoleType.getName(info.getDataPoint())== null?"":PointConsoleType.getName(info.getDataPoint()));
                        ((TextView) childRow.getChildAt(3)).setText(info.getDataPoint() == null?"": ""+info.getDataPoint());

                    }
                }
                break;
            }
            default:
                 break;
        }
    }

    /**
     *
     * @param pointType-刷新的点位类型：1-遥信，2-遥测，3-遥调，4-遥控
     */
    public static void freshPointModel(int pointType){

        /**
         * 如果新来的数据和当前显示是一类数据，则刷新显示
         */
        if (Host101PointState.pointChooseType == pointType){
            setTableData(Host101PointState.pointSource,pointType);
        }
    }
}
